<template>
	<view class="page">
		<view v-if="waybill" class="top-area">
			<view class="address-info">
				<xzfx-timeline>
					<xzfx-timeline-item>
						<view slot="icon" class="address-icon" style="background: #007aff;">装</view>
						<view class="address-name">{{ waybill.sendProvince || "" }} {{ waybill.sendCity || "" }}</view>
						<view class="address-full">{{ waybill.sendLocation || "" }}</view>
						<!-- <view class="address-simple">
							{{ waybill.receiveCity || "" }}
							{{ waybill.receiveDistrict || "" }}
						</view> -->
					</xzfx-timeline-item>
					<xzfx-timeline-item>
						<view slot="icon" class="address-icon" style="background: #dd524d;">卸</view>
						<view class="address-name">{{ waybill.receiveProvince || "" }} {{ waybill.receiveCity || "" }}</view>
						<view class="address-full">{{ waybill.receiveLocation || "" }}</view>
					</xzfx-timeline-item>
				</xzfx-timeline>
			</view>
			<view v-if="e.type === '1'" class="address-nav">
				<view class="item" @click.stop="toShipment">
					<div class="label">装货路线</div>
					<div class="icon xzfx_icon_route"></div>
				</view>
				<view class="split-line"></view>
				<view class="item" @click.stop="toDeliverGoods">
					<div class="label">送货路线</div>
					<div class="icon xzfx_icon_route"></div>
				</view>
			</view>
			<view v-if="e.type === '2'" class="address-nav">
				<view class="item" @click.stop="toShipment">
					<div class="label">去装货</div>
					<div class="icon xzfx_icon_navigation"></div>
				</view>
				<view class="split-line"></view>
				<view class="item" @click.stop="toDeliverGoods">
					<div class="label">去送货</div>
					<div class="icon xzfx_icon_navigation"></div>
				</view>
			</view>
		</view>

		<xzfx-panel v-if="waybill" title="车辆需求" isCard>
			<view slot="content" class="car-goods-info">
				<view class="item">
					<view class="label">所需车型:</view>
					<view class="value">{{ waybill.truckType || '-' }}</view>
				</view>
				<view class="item">
					<view class="label">整车拼车:</view>
					<view class="value">{{ waybill.shareStatus || "-" }}</view>
				</view>
			</view>
		</xzfx-panel>
		
		<xzfx-panel v-if="waybill" title="货物信息" isCard>
			<view slot="content" class="car-goods-info">
				<view class="item">
					<view class="label">货物类型:</view>
					<view class="value">{{ waybill.goodsType || '其他' }}</view>
				</view>
				<view class="item">
					<view class="label">货物体积:</view>
					<view class="value">{{ waybill.goodsVolume || 0 }}m³</view>
				</view>
				<view class="item">
					<view class="label">货物重量:</view>
					<view class="value">{{ waybill.goodsWeight || 0 }}kg</view>
				</view>
			</view>
		</xzfx-panel>
		
		<xzfx-panel v-if="waybill" title="发货人信息" isCard>
			<view slot="content" class="contact-info">
				<view class="item">
					<view class="label">发货人姓名:</view>
					<view class="value">
						{{ waybill.sendContackName || "" }}
					</view>
				</view>
				<view class="item">
					<view class="label">发货人电话:</view>
					<view class="value">
						{{ waybill.sendContackPhone | _maskPhone }}
						<text @click.stop="$call(waybill.sendContackPhone || '')"><text class="xzfx_icon_dial"></text>拨号</text>
					</view>
				</view>
				<view class="item">
					<view class="label">发货人地址:</view>
					<view class="value">
						{{ waybill.sendLocation || "" }}
					</view>
				</view>
			</view>
		</xzfx-panel>
		
		<xzfx-panel v-if="waybill" title="收货人信息" isCard>
			<view slot="content" class="contact-info">
				<view class="item">
					<view class="label">收货人姓名:</view>
					<view class="value">
						{{ waybill.receiveContackName || "" }}
					</view>
				</view>
				<view class="item">
					<view class="label">收货人电话:</view>
					<view class="value">
						{{ waybill.receiveContackPhone | _maskPhone }}
						<text @click.stop="$call(waybill.receiveContackPhone || '')"><text class="xzfx_icon_dial"></text>拨号</text>
					</view>
				</view>
				<view class="item">
					<view class="label">收货人地址:</view>
					<view class="value">
						{{ waybill.receiveLocation || "" }}
					</view>
				</view>
			</view>
		</xzfx-panel>
		
		<view v-if="waybill" class="footer-bar">
			<view v-if="e.type === '1'" class="price">
				<text style="padding-right: 10rpx;">报价:</text>
				{{ waybill.offerPrice || 0 }}
				<text>元</text>
			</view>
			<view v-if="e.type === '2'" class="price">
				<text style="padding-right: 10rpx;">净得运费:</text>
				{{ waybill.offerPrice || 0 }}
				<text>元</text>
			</view>
			<view class="footer-bar-btns">
				<view v-if="e.type === '2' && waybill.actionList.indexOf('取消接单') !== -1" class="btn" @click.stop="submitFn(waybill, 'truckCancel')">取消接单</view>
				<view v-if="e.type === '1' && waybill.actionList.indexOf('取消订单') !== -1" class="btn" @click.stop="submitFn(waybill, 'ownerCancel')">取消订单</view>
				<view v-if="e.type === '2' && waybill.actionList.indexOf('已取货') !== -1" class="btn" @click.stop="submitFn(waybill, 'take')">
					已取货
				</view>
				<view v-if="e.type === '2' && waybill.actionList.indexOf('已送达') !== -1" class="btn" @click.stop="submitFn(waybill, 'arrive')">
					已送达
				</view>
				<view v-if="e.type === '1' && waybill.actionList.indexOf('确认收货') !== -1" class="btn" @click.stop="submitFn(waybill, 'confirm')">
					确认收货
				</view>
				<view v-if="e.type === '1' && waybill.actionList.indexOf('去支付') !== -1" class="btn" @click.stop="payFn(waybill)">
					立即支付
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				e: null,
				waybill: null
			};
		},
		onLoad(e) {
			this.e = e;
			this.getDataFn()
		},
		methods: {
			toShipment(){
				let plugin = requirePlugin('routePlan');
				let key = 'XKEBZ-XT6AM-UMS6B-6VEJQ-INKE6-UYFAH';  //使用在腾讯位置服务申请的key
				let referer = '百川海融小程序';   //调用插件的app的名称
				let endPoint = JSON.stringify({  //终点
				    name: this.waybill.sendLocation,
				    latitude: Number(this.waybill.sendLat),
				    longitude: Number(this.waybill.sendLng)
				});
				wx.navigateTo({
				    url: 'plugin://routePlan/index?mode=driving&key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
				});
			},
			toDeliverGoods(){
				let plugin = requirePlugin('routePlan');
				let key = 'XKEBZ-XT6AM-UMS6B-6VEJQ-INKE6-UYFAH';  //使用在腾讯位置服务申请的key
				let referer = '百川海融小程序';   // 调用插件的app的名称
				let startPoint = JSON.stringify({  // 起点
				    name: this.waybill.sendLocation,
				    latitude: Number(this.waybill.sendLat),
				    longitude: Number(this.waybill.sendLng)
				});
				let endPoint = JSON.stringify({  //终点
				    name: this.waybill.receiveLocation,
				    latitude: Number(this.waybill.receiveLat),
				    longitude: Number(this.waybill.receiveLng)
				});
				wx.navigateTo({
				    url: 'plugin://routePlan/index?mode=driving&key=' + key + '&referer=' + referer + '&startPoint=' + startPoint + '&endPoint=' + endPoint
				});
			},
			submitFn(item, fn){
				this.$utils.system.loading("请稍后");
				this.$apis.waybill[fn]({
					orderId: item.id
				}).then(res => {
					this.getDataFn();
				}).catch(err => {
					console.log(err)
				}).finally(() => {
					uni.hideLoading();
				});
			},
			payFn(item){
				this.$utils.system.loading("请稍后");
				uni.login({
					success: response => {
						this.$apis.waybill.pay({
							orderId: item.id,
							code: response.code
						}).then(res => {
							uni.requestPayment({
								provider: "wxpay",
								orderInfo: res,
								timeStamp: String(res.timeStamp),
								nonceStr: res.nonceStr,
								package: res.package,
								signType: res.signType,
								paySign: res.paySign,
								success: (e) => {
									uni.redirectTo({
										url: "/pages/waybill/pay-result?type=success"
									});
								},
								fail: (e) => {
									uni.navigateTo({
										url: "/pages/waybill/pay-result?type=error"
									});
								},
								complete: () => {
									uni.hideLoading()
								}
							})
						}).catch(err => {
							console.log(err)
						}).finally(() => {
							uni.hideLoading();
						});
					}
				});
			},
			getDataFn() {
				this.$utils.system.loading("加载中，请稍后");
				this.$apis.waybill.getDetails(this.e).then(res => {
					this.waybill = res;
				}).catch(err => {
					console.log(err)
				}).finally(() => {
					uni.hideLoading();
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.page {
		padding-bottom: 150rpx;
		.top-area {
			margin: $xzfx-spacing;
			padding: $xzfx-spacing;
			background-color: #fff;
			border-radius: 20rpx;

			.address-info {
				background-color: #f6faff;
				padding: 20rpx 20rpx 0 20rpx;
				border-radius: 20rpx;

				.address-icon {
					width: 40rpx;
					height: 40rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					border-radius: 5rpx;
					color: #fff;
				}

				.address-name {
					font-size: 32rpx;
					font-weight: bold;
				}

				.address-full {
					margin-top: 5rpx;
					font-size: 28rpx;
				}

				.address-simple {
					margin-top: 10rpx;
					font-size: 26rpx;
					color: #999;
				}
			}

			.address-nav {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.split-line {
					width: 1rpx;
					height: 60rpx;
					background-color: #d7d7d7;
				}

				.item {
					width: 50%;
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 20rpx 20rpx 0rpx 20rpx;

					.label {
						font-size: 30rpx;
						color: #333333;
					}
					
					.icon {
						display: flex;
						justify-content: center;
						align-items: center;
						width: 80rpx;
						height: 80rpx;
						font-size: 50rpx;
						font-family: 500;
						background-color: #f6faff;
						border-radius: 10rpx;
						color: #f0ad4e;
					}
				}

				.item:first-child {
					padding-right: 40rpx;
				}

				.item:last-child {
					padding-left: 25rpx;
				}
			}
		}
		.contact-info{
			margin-top: $xzfx-spacing;
			padding-bottom: $xzfx-spacing;
			.item{
				display: flex;
				justify-content: space-between;
				padding: 5rpx 0;
				.value{
					width: calc(100% - 155rpx);
					font-size: 28rpx;
					color: #333333;
					text:only-child{
						margin-left: 20rpx;
						.xzfx_icon_dial{
							padding-right: 10rpx;
							color: $xzfx-theme;
						}
					}
				}
				.label{
					width: 145rpx;
					font-size: 28rpx;
					color: #999999;
					span{
						padding: 0 0.5em;
					}
				}
			}
		}
		.car-goods-info {
			margin-top: $xzfx-spacing;
			padding-bottom: $xzfx-spacing;
			.item {
				display: flex;
				font-size: 28rpx;
				padding: 5rpx 0;

				.label {
					width: 125rpx;
					color: #999999;
				}

				.value {
					width: calc(100% - 125rpx);
					display: flex;
					align-items: center;
					color: #333333;

					text {
						padding: 0 10rpx;
						font-size: 22rpx;
						color: #c9c9c9;
					}
				}
			}
		}


		.footer-bar {
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 2;
			width: 100%;
			height: 120rpx;
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 $xzfx-spacing;
			border-top: 1rpx solid #f1f1f1;

			.price {
				// height: 46rpx;
				font-size: 34rpx;
				font-weight: bold;
				color: red;
				display: flex;
				align-items: center;
				
				text {
					font-size: 28rpx;
					margin-left: 5rpx;
					color: #333333;
				}
			}
			
			.footer-bar-btns{
				display: flex;
				align-items: center;
				.btn {
					width: 200rpx;
					height: 80rpx;
					border-radius: 40rpx;
					color: #fff;
					background-color: $xzfx-theme;
					display: flex;
					justify-content: center;
					align-items: center;
					margin-left: 20rpx;
				}
			}

		}
	}
</style>